//
//  XNHomeTableViewController.swift
//  weibo
//
//  Created by 王向宁 on 16/5/19.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit
import SVProgressHUD

private let HomeCellId = "HomeCellId"

class XNHomeTableViewController: XNBaseTableViewController {
    lazy var statusListViewModel : XNStatusListViewModel = XNStatusListViewModel()
    //定义模型数组
   // lazy var statuses: [XNStatus] = [XNStatus]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //用户未登录 设置访客视图的信息
        if !userLogin {
            visitorLoginView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon",isHome: true)
            return
        }
        
        setNavBar()
        
        loadData()
        
        prepareForTableView()

    }
    
    private func prepareForTableView() {
        //注册
        tableView.registerClass(XNStatusCell.self, forCellReuseIdentifier: HomeCellId)
        
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估行高
        tableView.estimatedRowHeight = 300
        
        //给刷新控件实例化
        refreshControl = UIRefreshControl()
        //添加触发响应的事件
        refreshControl?.addTarget(self, action: #selector(XNHomeTableViewController.loadData), forControlEvents: .ValueChanged)
    }
    
    private func setNavBar() {
        
        let popItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(XNHomeTableViewController.push))
        
        navigationItem.rightBarButtonItem = popItem
    }
    
    @objc private func push() {
        
        let detail = XNDetailViewController()
        
//        detail.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(detail, animated: true)
    }

}


extension XNHomeTableViewController {
    
    @objc private func loadData() {
        statusListViewModel.loadData { (isSuccess) in
            //停止小菊花转动
            self.refreshControl?.endRefreshing()
            
            if !isSuccess {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                
                return
            }
            
            self.tableView.reloadData()
        }
    }
}

//        HMNetworkTools.sharedTools.loadHomePageData { (responseObject, error) in
//            if error != nil {
//                SVProgressHUD.showErrorWithStatus(AppErrorTip)
//                return
//            }
//            
//            //print(responseObject)
//            //处理数据AFN 已经将数据处理成 oc之后数组或字典
//            guard let dict = responseObject as? [String : AnyObject] else {
//                return
//            }
//            
//            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
//                return
//            }
//            
//            print(array)
//            
//            var tempArray = [XNStatus]()
//            
//            for item in array {
//                let s = XNStatus(dict: item)
//                
//                tempArray.append(s)
//            }
//            
//            self.statuses = tempArray
//            
//            self.tableView.reloadData()
//        }
//    }


//实现数据源方法
extension XNHomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusViewModelArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! XNStatusCell
        
        cell.statusViewModel = statusListViewModel.statusViewModelArray[indexPath.row]
   //打印被转发信息
//        print(statusListViewModel.statusViewModelArray[indexPath.row].status?.retweeted_status?.text)
        
        return cell
    }
}









